package com.intellagric.service.iftmonitor.Impl.ift_scene;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.iftmonitor.SceneDao;
import com.intellagric.mapper.IftSceneMapper;
import com.intellagric.module.iftmonitor.SceneVo;
import com.intellagric.pojo.IftScene;
import com.intellagric.pojo.IftSceneExample;
import com.intellagric.service.iftmonitor.ift_scene.SceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 场景信息管理服务
 */
@Service
public class SceneServiceImpl implements SceneService {

    @Autowired
    private IftSceneMapper sceneMapper;

    @Autowired
    private SceneDao sceneDao;

    /**
     * 添加场景信息
     * @param scene
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addScene(IftScene scene) {
        if (sceneMapper.insertSelective(scene) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除场景信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteScene(String id) {
        if (sceneMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑场景信息
     * @param scene
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editScene(IftScene scene) {
        if (sceneMapper.updateByPrimaryKeySelective(scene) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询场景信息
     * @param id
     * @return IftScene
     */
    @Override
    public IftScene getSceneById(String id) {
        return sceneMapper.selectByPrimaryKey(id);
    }

    /**
     * 多条件查询场景信息
     * @param sceneVo
     * @return IftScene
     */
    @Override
    public LayuiDataGridResult getSceneByVo(SceneVo sceneVo) {
        List<IftScene> sceneList = sceneDao.selectSceneByVO(sceneVo);
        return LayuiDataGridResult.success().add(sceneList,sceneList.size());
    }

    /**
     * 查询场景信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneList() {
        IftSceneExample sceneExample = new IftSceneExample();
        sceneExample.setOrderByClause("sort");
        sceneExample.createCriteria().andIdIsNotNull();
        List<IftScene> sceneList = sceneMapper.selectByExample(sceneExample);
        return LayuiDataGridResult.success().add(sceneList,sceneList.size());
    }

    /**
     * 分页查询场景信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getScenePage(int page, int rows) {
        IftSceneExample sceneExample = new IftSceneExample();
        sceneExample.createCriteria().andIdIsNotNull();
        List<IftScene> sceneList = sceneMapper.selectByExample(sceneExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(sceneList);
        return result;
    }

    /**
     * 查询用户下场景信息列表,生成树形信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneTreeByUser(String user) {
        //父级场景列表
        IftSceneExample sceneExample1 = new IftSceneExample();
        sceneExample1.setOrderByClause("sort");
        sceneExample1.createCriteria().andParentIdEqualTo("0").andCreateByEqualTo(user);
        List<IftScene> sceneList = sceneMapper.selectByExample(sceneExample1);

        //所有场景列表
        IftSceneExample sceneExample = new IftSceneExample();
        sceneExample.setOrderByClause("sort");
        sceneExample.createCriteria().andParentIdEqualTo("0").andCreateByEqualTo(user);
        List<IftScene> rootsceneList = sceneMapper.selectByExample(sceneExample);

        for (IftScene scene : rootsceneList ) {
            scene.setChildren(getChild(scene.getId(),sceneList));
        }
        return LayuiDataGridResult.success().add(rootsceneList,rootsceneList.size());
    }

    /**
     * 查询场景信息列表,生成树形信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneTree() {

        List<IftScene> sceneList = this.getSceneList().getData();

        IftSceneExample sceneExample = new IftSceneExample();
        sceneExample.setOrderByClause("sort");
        sceneExample.createCriteria().andParentIdEqualTo("0");
        List<IftScene> rootsceneList = sceneMapper.selectByExample(sceneExample);

        for (IftScene scene : rootsceneList ) {
            scene.setChildren(getChild(scene.getId(),sceneList));
        }
        return LayuiDataGridResult.success().add(rootsceneList,rootsceneList.size());
    }

    /**
     * 递归查询子分类
     * @param id
     * @param sceneList
     * @return ResponseMessage
     */
    private List<IftScene> getChild(String id, List<IftScene> sceneList) {

        //返回的子菜单
        List<IftScene> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftScene scene : sceneList) {
            if (scene.getParentId() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (scene.getParentId().equals(id)) {
                    childList.add(scene);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (IftScene scene : childList) {
            scene.setChildren(getChild(scene.getId(),sceneList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }


}
